package org.batatasframework.support.charts.baidu;

import java.util.List;
import java.util.Map;

import org.batatasframework.support.charts.ResultArrayReader;
import org.batatasframework.support.charts.ResultDataReader;
import org.batatasframework.support.charts.db.DBChartArray;
import org.batatasframework.support.charts.db.DBChartData;
import org.springframework.util.Assert;


public class BaiduUtils {
	
	//饼图
	public static ChartOption pie(String name, ResultDataReader reader){
//		Assert.isTrue(lendData.length == value.length, "name.length != value.length");

		DBChartData data = new DBChartData(reader);
		String[] lendData = data.getLabels();
		Object[] value = data.getData();
		ChartOption option = new ChartOption();
		ChartTitle chartTitle = new ChartTitle();
		chartTitle.setText(name);
		option.setTitle(chartTitle);
		
		ChartTooltip chartTooltip = new ChartTooltip();
		chartTooltip.setTrigger("item");
		chartTooltip.setFormatter("{a} <br/>{b} : {c} ({d}%)");
		option.setTooltip(chartTooltip);
		
		ChartLegend legend = new ChartLegend();
//		legend.getData().addAll(data.getData());
		for(String st : lendData){
			legend.getData().add(st);
		}
		option.setLegend(legend);
		
		ChartSeriePie csp = new ChartSeriePie();
		/*csp.setRadius("50%");
		csp.setCenter("50%", "70%");*/
		csp.setName(name);
		for(int i = 0; i < lendData.length; i++){
			csp.getData().add(new ChartSeriePieDataItem(lendData[i], value[i]));
		}
		
		option.getSeries().add(csp);
		return option;
	}
	//饼图
	public static ChartOption pie(String name, String[] lendData, Object[] value){
		Assert.isTrue(lendData.length == value.length, "name.length != value.length");
		ChartOption option = new ChartOption();
		
		ChartTitle chartTitle = new ChartTitle();
		chartTitle.setText(name);
		option.setTitle(chartTitle);
		
		ChartTooltip chartTooltip = new ChartTooltip();
		chartTooltip.setTrigger("item");
		chartTooltip.setFormatter("{a} <br/>{b} : {c} ({d}%)");
		option.setTooltip(chartTooltip);
		
		ChartLegend legend = new ChartLegend();
		for(String st : lendData){
			legend.getData().add(st);
		}
		option.setLegend(legend);
		
		ChartSeriePie csp = new ChartSeriePie();
		/*csp.setRadius("50%");
		csp.setCenter("50%", "70%");*/
		csp.setName(name);
		for(int i = 0; i < lendData.length; i++){
			csp.getData().add(new ChartSeriePieDataItem(lendData[i], value[i]));
		}
		
		option.getSeries().add(csp);
		return option;
	}
	
	//柱状图
	public static ChartOption bar(String name,String formatter,
			ResultArrayReader reader
			//String[] lendData, Object[][] value,Object[] xAxisData
			){
//			Assert.isTrue(lendData.length == value.length, "lendData.length != value.length");
		DBChartArray data = new DBChartArray(reader);
		String[] lendData = data.getLabels();
		Object[][] value = data.getData();
		Object[] xAxisData = data.getAxis();
		
		ChartOption option = new ChartOption();
		
		ChartTitle chartTitle = new ChartTitle();
		chartTitle.setText(name);
		option.setTitle(chartTitle);
		
		ChartToolbox  chartToolbox = new ChartToolbox();
		chartToolbox.setShow(true);
		option.setToolbox(chartToolbox);
		
		ChartTooltip chartTooltip = new ChartTooltip();
		chartTooltip.setTrigger("axis");
		option.setTooltip(chartTooltip);
		
		ChartLegend chartLegend = new ChartLegend();
		for (String str : lendData) {
			chartLegend.getData().add(str);
		}
		option.setLegend(chartLegend);
		
		ChartAxisCategory chartAxisCategory = new ChartAxisCategory(xAxisData);
		
		option.getxAxis().add(chartAxisCategory);
		
		
		ChartAxisValue chartAxisValue = new ChartAxisValue();
		chartAxisValue.getAxisLabel().setShow(true);
		chartAxisValue.setMin("0");
		chartAxisValue.getAxisLabel().setFormatter(formatter);
		option.getyAxis().add(chartAxisValue);
		
		for (int i=0 ; i<lendData.length ; i++) {
			ChartSerieBar serieBar = new ChartSerieBar();
			serieBar.setName(lendData[i]);
			
			Object[] vs = value[i];
			for(Object o : vs){
				serieBar.getData().add(o);
			}
			option.getSeries().add(serieBar);
		}
		return option;
	}
	
	//柱状图
	public static ChartOption bar(String name,String formatter,String[] lendData, Object[][] value,Object[] xAxisData){
		Assert.isTrue(lendData.length == value.length, "lendData.length != value.length");
		ChartOption option = new ChartOption();
		
		ChartTitle chartTitle = new ChartTitle();
		chartTitle.setText(name);
		option.setTitle(chartTitle);
		
		ChartToolbox  chartToolbox = new ChartToolbox();
		chartToolbox.setShow(true);
		option.setToolbox(chartToolbox);
		
		ChartTooltip chartTooltip = new ChartTooltip();
		chartTooltip.setTrigger("axis");
		option.setTooltip(chartTooltip);
		
		ChartLegend chartLegend = new ChartLegend();
		for (String str : lendData) {
			chartLegend.getData().add(str);
		}
		option.setLegend(chartLegend);
		
		ChartAxisCategory chartAxisCategory = new ChartAxisCategory(xAxisData);
		
		option.getxAxis().add(chartAxisCategory);
		
		
		ChartAxisValue chartAxisValue = new ChartAxisValue();
		chartAxisValue.getAxisLabel().setShow(true);
		chartAxisValue.setMin("0");
		chartAxisValue.getAxisLabel().setFormatter(formatter);
		option.getyAxis().add(chartAxisValue);
		
		for (int i=0 ; i<lendData.length ; i++) {
			ChartSerieBar serieBar = new ChartSerieBar();
			serieBar.setName(lendData[i]);
			
			Object[] vs = value[i];
			for(Object o : vs){
				serieBar.getData().add(o);
			}
			option.getSeries().add(serieBar);
		}
		return option;
	}
	//折线图
	public static ChartOption line(String name,String formatter, ResultArrayReader reader){
//		Assert.isTrue(lendData.length == value.length, "name.length != value.length");
		DBChartArray data = new DBChartArray(reader);
		String[] lendData = data.getLabels();
		Object[][] value = data.getData();
		Object[] xAxisData = data.getAxis();
		
		ChartOption option = new ChartOption();
		
		ChartTitle chartTitle = new ChartTitle();
		chartTitle.setText(name);
		option.setTitle(chartTitle);
		
		ChartTooltip chartTooltip = new ChartTooltip();
		chartTooltip.setTrigger("axis");
		option.setTooltip(chartTooltip);
	
		ChartLegend chartLegend = new ChartLegend();
		for (String str : lendData) {
			chartLegend.getData().add(str);
		}
		option.setLegend(chartLegend);
		
		ChartToolbox chartToolbox = new ChartToolbox();
		chartToolbox.setShow(true);
		option.setToolbox(chartToolbox);
		
		ChartAxisCategory xAxis = new ChartAxisCategory(xAxisData);
		option.getxAxis().add(xAxis);
		
		ChartAxisValue yAxis = new ChartAxisValue();
		ChartAxisLabel axisLabel = new ChartAxisLabel();
		axisLabel.setShow(true);
		axisLabel.setFormatter(formatter);
		yAxis.setAxisLabel(axisLabel);
		option.getyAxis().add(yAxis);
		
		for (int i = 0; i < lendData.length; i++) {
		ChartSerieLine serieLine = new ChartSerieLine();
			serieLine.setName(lendData[i]);
			Object[] object = value[i];
			for (Object obj : object) {
				serieLine.getData().add(obj);
			}
			option.getSeries().add(serieLine);
		}
		return option;
	}
	//折线图
	public static ChartOption line(String name,String formatter,String[] lendData, Object[][] value,Object[] xAxisData ){
		Assert.isTrue(lendData.length == value.length, "name.length != value.length");
		ChartOption option = new ChartOption();
		
		ChartTitle chartTitle = new ChartTitle();
		chartTitle.setText(name);
		option.setTitle(chartTitle);
		
		ChartTooltip chartTooltip = new ChartTooltip();
		chartTooltip.setTrigger("axis");
		option.setTooltip(chartTooltip);
	
		ChartLegend chartLegend = new ChartLegend();
		for (String str : lendData) {
			chartLegend.getData().add(str);
		}
		option.setLegend(chartLegend);
		
		ChartToolbox chartToolbox = new ChartToolbox();
		chartToolbox.setShow(true);
		option.setToolbox(chartToolbox);
		
		ChartAxisCategory xAxis = new ChartAxisCategory(xAxisData);
		option.getxAxis().add(xAxis);
		
		ChartAxisValue yAxis = new ChartAxisValue();
		ChartAxisLabel axisLabel = new ChartAxisLabel();
		axisLabel.setShow(true);
		axisLabel.setFormatter(formatter);
		yAxis.setAxisLabel(axisLabel);
		option.getyAxis().add(yAxis);
		
		for (int i = 0; i < lendData.length; i++) {
		ChartSerieLine serieLine = new ChartSerieLine();
			serieLine.setName(lendData[i]);
			Object[] object = value[i];
			for (Object obj : object) {
				serieLine.getData().add(obj);
			}
			option.getSeries().add(serieLine);
		}
		return option;
	}
	//两条y轴柱状图
	public static ChartOption moreYAxisbar(String name,String[] lendData, Object[][] value,Object[] xAxisData,int useFirstYAxis){
		Assert.isTrue(lendData.length == value.length, "name.length != value.length");
		ChartOption option = new ChartOption();
		
		ChartTitle chartTitle = new ChartTitle();
		chartTitle.setText(name);
		option.setTitle(chartTitle);
		
		ChartToolbox  chartToolbox = new ChartToolbox();
		chartToolbox.setShow(true);
		option.setToolbox(chartToolbox);
		
		ChartTooltip chartTooltip = new ChartTooltip();
		chartTooltip.setTrigger("axis");
		option.setTooltip(chartTooltip);
		
		ChartLegend chartLegend = new ChartLegend();
		for (String str : lendData) {
			chartLegend.getData().add(str);
		}
		option.setLegend(chartLegend);
		
		
		ChartAxisCategory chartAxisCategory = new ChartAxisCategory(xAxisData);
		
		option.getxAxis().add(chartAxisCategory);
		
		for (int i = 0; i < 2; i++) {
			ChartAxisValue chartAxisValue = new ChartAxisValue();
			chartAxisValue.getAxisLabel().setShow(true);
			option.getyAxis().add(chartAxisValue);
		}
		
		for (int i=0 ; i<lendData.length ; i++) {
			ChartSerieBar serieBar = new ChartSerieBar();
			serieBar.setName(lendData[i]);
			
			Object[] vs = value[i];
			for(Object o : vs){
				serieBar.getData().add(o);
			}
			if(i>=useFirstYAxis){
				serieBar.setyAxisIndex(1);
			}
			option.getSeries().add(serieBar);
		}
		return option;
	}
	//柱状图与折线图(一柱一折) 两Y轴
	public static ChartOption barAndLine(String name,String[] lendData, Object[][] value,Object[] xAxisData,int useFirstYAxis){
			Assert.isTrue(lendData.length == value.length, "name.length != value.length");
			ChartOption option = new ChartOption();
			
			ChartTitle chartTitle = new ChartTitle();
			chartTitle.setText(name);
			option.setTitle(chartTitle);
			
			ChartToolbox  chartToolbox = new ChartToolbox();
			chartToolbox.setShow(true);
			option.setToolbox(chartToolbox);
			
			ChartTooltip chartTooltip = new ChartTooltip();
			chartTooltip.setTrigger("axis");
			option.setTooltip(chartTooltip);
			
			ChartLegend chartLegend = new ChartLegend();
			for (String str : lendData) {
				chartLegend.getData().add(str);
			}
			option.setLegend(chartLegend);
			
			ChartAxisCategory chartAxisCategory = new ChartAxisCategory(xAxisData);
			
			option.getxAxis().add(chartAxisCategory);
			
			for (int i = 0; i < 2; i++) {
				ChartAxisValue chartAxisValue = new ChartAxisValue();
				chartAxisValue.getAxisLabel().setShow(true);
				option.getyAxis().add(chartAxisValue);
			}
			for (int i=0 ; i<lendData.length ; i++) {
				if(i==0){
					ChartSerieBar serieBar = new ChartSerieBar();
					serieBar.setName(lendData[i]);				
					Object[] vs = value[i];
					for(Object o : vs){
						serieBar.getData().add(o);
					}
					option.getSeries().add(serieBar);
				}
				if(i==1){
					ChartSerieLine serieLine = new ChartSerieLine();
					serieLine.setName(lendData[i]);				
					Object[] vs = value[i];
					for(Object o : vs){
						serieLine.getData().add(o);
					}
					serieLine.setyAxisIndex(1);
					option.getSeries().add(serieLine);
				}				
			}
			return option;
		}
	//柱状图 X轴与Y轴互换 
	public static ChartOption barXvalueYcategory(String name,String formatter,String[] lendData, Object[][] value,Object[] yAxisData,int xAxis){
		Assert.isTrue(lendData.length == value.length, "name.length != value.length");
		ChartOption option = new ChartOption();
		
		ChartTitle chartTitle = new ChartTitle();
		chartTitle.setText(name);
		option.setTitle(chartTitle);
		
		ChartToolbox  chartToolbox = new ChartToolbox();
		chartToolbox.setShow(true);
		option.setToolbox(chartToolbox);
		
		ChartTooltip chartTooltip = new ChartTooltip();
		chartTooltip.setTrigger("axis");
		option.setTooltip(chartTooltip);
		
		ChartLegend chartLegend = new ChartLegend();
		for (String str : lendData) {
			chartLegend.getData().add(str);
		}
		option.setLegend(chartLegend);
		
		ChartAxisCategory chartAxisCategory = new ChartAxisCategory(yAxisData);
		
		option.getyAxis().add(chartAxisCategory);
		
		for (int i = 0; i <xAxis; i++) {
			ChartAxisValue chartAxisValue = new ChartAxisValue();
			chartAxisValue.getAxisLabel().setShow(true);
			chartAxisValue.setMin("0");
			chartAxisValue.getAxisLabel().setFormatter(formatter);
			option.getxAxis().add(chartAxisValue);
		}
		
		
		for (int i=0 ; i<lendData.length ; i++) {
			ChartSerieBar serieBar = new ChartSerieBar();
			serieBar.setName(lendData[i]);
			Object[] vs = value[i];
				for(Object o : vs){
					serieBar.getData().add(o);
			}
			
			option.getSeries().add(serieBar);
		}
		return option;
	}
	//仪表盘
	public static ChartOption gauge(String name,String tooltipFormatter,String detailFormatter, ChartGaugeData value){
		
		ChartOption option = new ChartOption();
		
		ChartTooltip tooltip = new ChartTooltip();
		tooltip.setFormatter(tooltipFormatter);
		option.setTooltip(tooltip);
			
		ChartSerieGauge serieGauge = new ChartSerieGauge();
		serieGauge.setName(name);
		ChartDetail chartDetail = new ChartDetail();
		chartDetail.setFormatter(detailFormatter);
		serieGauge.setDetail(chartDetail);
		serieGauge.getData().add(value);
		option.getSeries().add(serieGauge);
		return option;
	}
	//仪表盘
	public static ChartOption gauge(String name,String tooltipFormatter,String detailFormatter, Object value){
		
		ChartOption option = new ChartOption();
		
		ChartTooltip tooltip = new ChartTooltip();
		tooltip.setFormatter(tooltipFormatter);
		option.setTooltip(tooltip);
			
		ChartSerieGauge serieGauge = new ChartSerieGauge();
		serieGauge.setName(name);
		ChartDetail chartDetail = new ChartDetail();
		chartDetail.setFormatter(detailFormatter);
		serieGauge.setDetail(chartDetail);
		serieGauge.getData().add(value);
		option.getSeries().add(serieGauge);
		return option;
	}
	//柱状图与折线图(多柱一折) 两Y轴
public static ChartOption moreBarAndLine(String name,String[] lendData, Object[][] value,Object[] xAxisData,int useFirstYAxis){
		Assert.isTrue(lendData.length == value.length, "name.length != value.length");
		ChartOption option = new ChartOption();
		
		ChartTitle chartTitle = new ChartTitle();
		chartTitle.setText(name);
		option.setTitle(chartTitle);
		
		ChartToolbox  chartToolbox = new ChartToolbox();
		chartToolbox.setShow(true);
		option.setToolbox(chartToolbox);
		
		ChartTooltip chartTooltip = new ChartTooltip();
		chartTooltip.setTrigger("axis");
		option.setTooltip(chartTooltip);
		
		ChartLegend chartLegend = new ChartLegend();
		for (String str : lendData) {
			chartLegend.getData().add(str);
		}
		option.setLegend(chartLegend);
		
		ChartAxisCategory chartAxisCategory = new ChartAxisCategory(xAxisData);
		
		option.getxAxis().add(chartAxisCategory);
		
		for (int i = 0; i < 2; i++) {
			ChartAxisValue chartAxisValue = new ChartAxisValue();
			chartAxisValue.getAxisLabel().setShow(true);
			option.getyAxis().add(chartAxisValue);
		}
		for (int i=0 ; i<lendData.length ; i++) {
			if(i<useFirstYAxis){
				ChartSerieBar serieBar = new ChartSerieBar();
				serieBar.setName(lendData[i]);				
				Object[] vs = value[i];
				for(Object o : vs){
					serieBar.getData().add(o);
				}
				option.getSeries().add(serieBar);
			}else{
				ChartSerieLine serieLine = new ChartSerieLine();
				
				serieLine.setName(lendData[i]);				
				Object[] vs = value[i];
				for(Object o : vs){
					serieLine.getData().add(o);
				}
				serieLine.setyAxisIndex(1);
				option.getSeries().add(serieLine);
			}				
		}
		return option;
	}
	//漏斗图(两图对比)
	public static ChartOption funnnel(String title,String[] serieName,Object[][]  value,String[] lendData){
		
		ChartOption option = new ChartOption();
		ChartTitle chartTitle = new ChartTitle();
		chartTitle.setText(title);
		option.setTitle(chartTitle);
		
		ChartLegend chartLegend = new ChartLegend();
		for (String str : lendData) {
			chartLegend.getData().add(str);
		}
		option.setLegend(chartLegend);
		
		ChartTooltip chartTooltip = new ChartTooltip();
		chartTooltip.setTrigger("item");
		option.setTooltip(chartTooltip);
		
		for (int i = 0; i < value.length; i++) {
			ChartSerieFunnel serieFunnel = new ChartSerieFunnel();
			serieFunnel.setName(serieName[i]);
			Object[] vs = value[i];
			for (int j = 0; j < value[i].length; j++) {
				serieFunnel.getData().add(vs[j]);
			}
			option.getSeries().add(serieFunnel);
			
		}
		
		return option;
	}
	//雷达图
	public static ChartOption rader(String title,String formatter,String[] lendData,String[] serieName,List<ChartRadarIndicator> radarData,Object[]  value){
		
		ChartOption option = new ChartOption();
		ChartTitle chartTitle = new ChartTitle();
		chartTitle.setText(title);
		option.setTitle(chartTitle);
		
		ChartTooltip tooltip = new ChartTooltip();
		tooltip.setFormatter(formatter);
		option.setTooltip(tooltip);
		
		ChartLegend chartLegend = new ChartLegend();
		for (String str : lendData) {
			chartLegend.getData().add(str);
		}
		option.setLegend(chartLegend);
		
		ChartRadar radar = new ChartRadar();
		for (int i = 0; i < radarData.size(); i++) {
			ChartRadarIndicator indicator = (ChartRadarIndicator)radarData.get(i);
			radar.getIndicator().add(indicator);
		}
		option.setRadar(radar);
		

			ChartSerieRadar serieRadar = new ChartSerieRadar();
			serieRadar.setName(lendData[0]);
			
				serieRadar.getData().add(value);
			option.getSeries().add(serieRadar);
			
	
		
		return option;
	}
	//柱状图
		public static ChartOption barAndPie(String name,String formatter,
				ResultArrayReader reader,
				//String[] lendData, Object[][] value,Object[] xAxisData
				Object[] pieValue){
//				Assert.isTrue(lendData.length == value.length, "lendData.length != value.length");
			DBChartArray data = new DBChartArray(reader);
			String[] lendData = data.getLabels();
			Object[][] value = data.getData();
			Object[] xAxisData = data.getAxis();
		//	Assert.isTrue(lendData.length == (value.length-1), "lendData.length != value.length-1");
			ChartOption option = new ChartOption();
			
			ChartTitle chartTitle = new ChartTitle();
			chartTitle.setText(name);
			option.setTitle(chartTitle);
			
			ChartToolbox  chartToolbox = new ChartToolbox();
			chartToolbox.setShow(true);
			option.setToolbox(chartToolbox);
			
			ChartTooltip chartTooltip = new ChartTooltip();
			
			chartTooltip.setTrigger("item");
			option.setTooltip(chartTooltip);
			
			ChartLegend chartLegend = new ChartLegend();
			for (String str : lendData) {
				chartLegend.getData().add(str);
			}
			option.setLegend(chartLegend);
			
			ChartAxisCategory chartAxisCategory = new ChartAxisCategory(xAxisData);
			
			option.getxAxis().add(chartAxisCategory);
			
			
			ChartAxisValue chartAxisValue = new ChartAxisValue();
			chartAxisValue.getAxisLabel().setShow(true);
			chartAxisValue.setMin("0");
			chartAxisValue.getAxisLabel().setFormatter(formatter);
			option.getyAxis().add(chartAxisValue);
			
			for (int i=0 ; i<lendData.length ; i++) {
				ChartSerieBar serieBar = new ChartSerieBar();
				serieBar.setName(lendData[i]);
				
				Object[] vs = value[i];
				for(Object o : vs){
					serieBar.getData().add(o);
				}
				option.getSeries().add(serieBar);
			}
			ChartSeriePie seriePie = new ChartSeriePie();
			seriePie.setName(name);
			for(int i = 0; i < lendData.length; i++){
				ChartData piel = new ChartData(lendData[i], (int)pieValue[i]);
				seriePie.getData().add(piel);
			}
			option.getSeries().add(seriePie);
			return option;
		}
		public static ChartOption multipleDimensionsBar(String name,String formatter,String[] lendData, Map<String, int[]> values,Object[] axisCategoryData){
			ChartOption option = new ChartOption();
			ChartTitle chartTitle = new ChartTitle();
			chartTitle.setText(name);
			option.setTitle(chartTitle);
			
			ChartToolbox  chartToolbox = new ChartToolbox();
			chartToolbox.setShow(true);
			option.setToolbox(chartToolbox);
			ChartTooltip tooltip = new ChartTooltip();
			tooltip.setTrigger("axis");
			ChartAxisPointer axisPointer = new ChartAxisPointer();
			axisPointer.setType("shadow");
			option.setTooltip(tooltip);
			tooltip.setAxisPointer(axisPointer);
			ChartLegend legend = new ChartLegend();
			for (int i = 0; i < lendData.length; i++) {
				legend.getData().add(lendData[i]);
				ChartSerieBar serieBar = new ChartSerieBar();
				int[] value = values.get(lendData[i]);
				serieBar.setName(lendData[i]);
				serieBar.setStack("总量");
				serieBar.getLabel().getNormal().getTextStyle().setColor("#fff");
				serieBar.getLabel().getNormal().setShow(true);
				serieBar.getLabel().getNormal().setPosition("insideLeft");
				for (int v : value) {
					serieBar.getData().add(v);
				}
				option.getSeries().add(serieBar);
			}
			option.setLegend(legend);
			ChartAxisCategory chartAxisCategory = new ChartAxisCategory(axisCategoryData);
			
			option.getyAxis().add(chartAxisCategory);
			
			
			ChartAxisValue chartAxisValue = new ChartAxisValue();
			chartAxisValue.getAxisLabel().setShow(true);
			chartAxisValue.setMin("0");
			chartAxisValue.getAxisLabel().setFormatter(formatter);
			option.getxAxis().add(chartAxisValue);
			return option;
		}
}
